package com.zcpt.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.zcpt.dao.PartnerDao;
import com.zcpt.exception.AlterException;
import com.zcpt.exception.InsertException;
import com.zcpt.exception.SystemException;
import com.zcpt.pojo.Partner;
import com.zcpt.service.PartnerService;

@Service("partnerService")
public class PartnerServiceImpl implements PartnerService {
	@Resource // 自动注入dao
	private PartnerDao partnerDao;

	@Override // 根据id查找参与者信息
	public Partner getPartnerById(int partnerId) {
		return partnerDao.queryPartnerById(partnerId);
	}

	@Override // 根据id查找参与者和项目信息
	public Partner getPartnerByIdWithProject(int partnerId) {
		return partnerDao.queryPartnerByIdWithProject(partnerId);
	}

	@Override // 查找所有参与者信息
	public List<Partner> getAllPartner() {
		return partnerDao.queryAllPartner();
	}

	@Override // 是否成功添加参与者信息
	public boolean addPartner(Partner partner) {
		try {
			// 添加信息
			int insertCount = partnerDao.insertPartner(partner);
			// 判断
			if (insertCount <= 0) {
				throw new InsertException("插入数据失败！");
			} else {
				return true;
			}
		} catch (InsertException e) {
			throw e;
		} catch (Exception e) {
			throw new SystemException("系统故障:" + e.getMessage());
		}
	}

	@Override // 参与者是否成功关联参与者和项目数据
	public boolean partnerProject(int partnerId, int projectId) {
		try {
			// 关联信息
			int withCount = partnerDao.insertPartnerWithProject(partnerId, projectId);
			// 判断
			if (withCount <= 0) {
				throw new InsertException("数据关联失败！");
			} else {
				return true;
			}
		} catch (InsertException e) {
			throw e;
		} catch (Exception e) {
			throw new SystemException("系统故障:" + e.getMessage());
		}
	}

	@Override // 是否成功修改参与者信息
	public boolean alterPartner(Partner partner) {
		try {
			// 修改数据
			int updateCount = partnerDao.updatePartner(partner);
			// 判断
			if (updateCount <= 0) {
				throw new AlterException("修改数据失败！");
			} else {
				return true;
			}
		} catch (AlterException e) {
			throw e;
		} catch (Exception e) {
			throw new SystemException("系统故障:" + e.getMessage());
		}
	}

}
